
# coding=utf-8

import os
import re
import json
import config
import pandas as pd
import numpy as np
from xpinyin import Pinyin
from dw.dw import *
dw = DwUtil()

# # 个性化拓展字典
# database_helper_dict = {
#     '20192020_nmntdfsx':unstack_20192020_nmntdfsx
# }

# 初始化pinyin
p = Pinyin()

# read database info
with open('data/json/dw_data.json', encoding='utf-8') as f:
    database_info = json.load(f)
    database_info = eval(database_info)


# 筛选数据，并返回
def query_data(form_dict):
    table_name = form_dict['db_name']
    c_c_dict = database_info[table_name]['col_name']
    select_lst = list(c_c_dict.keys())
    # print(select_lst)
    # print("********")
    where_str = ''
    # print("form_dict")
    # print(form_dict)
    form_form_dict = {}
    data = dict(form_dict)
    for iterr in data:
        SStr = ""
        for i in form_dict.getlist(iterr):
            SStr += i + '@'
        SStr = SStr[:-1]
        form_form_dict[iterr] = SStr
    # print(form_form_dict)
    form_map = {'res1': 'company', 'res2': 'system_name', 'res3': 'result', 'res4': 'num_core',
                'res6': 'cpu_speed', 'res7': 'first_cache', 'res8': 'second_cache',
                'res9': 'third_cache', 'res10': 'other_cache', 'res11': 'memory',
                'res12': 'updated', 'res13':'id', 'res5':'processor'}
    # print(list(form_form_dict.items()))
    for coll, query in list(form_form_dict.items())[1:]:

        # print("coll这是一次")
        # print(coll,query)
        # print("******")
        col = form_map[coll]


        if '@' in query:
            query = query.split('@')
            # print("yes")
            # print(query)
            Str = '('
            for iter in query:
                if type(iter)==int:
                    Str = Str + col + iter + ' OR '
                else:
                    Str = Str + col + '=\'' + iter + '\'' ' OR '
            Str = Str[:-3]
            Str = Str + ')'
            where_str = where_str + Str + ' AND '
            # print("where_str")
            # print(where_str)
        elif col=='db_name':
            pass
        else:
            where_str = where_str + col + '=\'' + query + '\'' + ' AND '


    select_str = ','.join(select_lst)
    where_str = where_str[:-4]
    if where_str:
        query_str = f'SELECT * FROM {table_name} WHERE {where_str}'
    else:
        query_str = f'SELECT * FROM {table_name}'
    # print(query_str)
    # print(query_str)
    result = dw.execute_sql(query_str)
    # print("result:")
    # print(result)
    return result


# 筛选数据，并返回
def query_processor(form_dict):
    table_name = form_dict['db_name']
    c_c_dict = database_info[table_name]['col_name']
    select_lst = list(c_c_dict.keys())
    # print(select_lst)
    # print("********")
    where_str = ''
    # print("form_dict")
    # print(form_dict)
    form_form_dict = {}
    data = dict(form_dict)
    for iterr in data:
        SStr = ""
        for i in form_dict.getlist(iterr):
            SStr += i + '@'
        SStr = SStr[:-1]
        form_form_dict[iterr] = SStr
    # print(form_form_dict)
    form_map = {'res1': 'company', 'res2': 'system_name', 'res3': 'result', 'res4': 'num_core',
                'res6': 'cpu_speed', 'res7': 'first_cache', 'res8': 'second_cache',
                'res9': 'third_cache', 'res10': 'other_cache', 'res11': 'memory',
                'res12': 'updated', 'res13':'id', 'res5':'processor'}
    # print(list(form_form_dict.items()))
    for coll, query in list(form_form_dict.items())[1:]:

        # print("coll这是一次")
        # print(coll,query)
        # print("******")
        col = form_map[coll]


        if '@' in query:
            query = query.split('@')
            # print("yes")
            # print(query)
            Str = '('
            for iter in query:
                if type(iter)==int:
                    Str = Str + col + iter + ' OR '
                else:
                    Str = Str + col + '=\'' + iter + '\'' ' OR '
            Str = Str[:-3]
            Str = Str + ')'
            where_str = where_str + Str + ' AND '
            # print("where_str")
            # print(where_str)
        elif col=='db_name':
            pass
        else:
            where_str = where_str + col + '=\'' + query + '\'' + ' AND '


    select_str = ','.join(select_lst)
    where_str = where_str[:-4]
    if where_str:
        query_str = f'SELECT * FROM {table_name} WHERE {where_str}'
    else:
        query_str = f'SELECT * FROM {table_name}'
    # print(query_str)
    # print(query_str)
    result = dw.execute_sql(query_str)
    # print("result:")
    # print(result)
    return result